using System;
using eShop.Business;
using System.Collections.Generic;
using Ext.Net;
using Entity;
using eShop.Model;


public partial class admin_sales_ccfail : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindOrders(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    private void BindOrders(string sort, int start, int limit)
    {
        List<Order> data = null;
        int totalCount = 0;
        string sqlCondition = string.Empty;
        start = start + 1;

        if (String.IsNullOrEmpty(sort))
            sort = "OrderId";

        Int32 _userId = -1;
        DateTime? fromDate = null;
        DateTime? toDate = null;

        if (this._useSearch)
        {
            if (!string.IsNullOrEmpty(txtOrderNo.Text))
            {
                _userId = Convert.ToInt32(txtOrderNo.Text.ToString());
            }


            if (txtDateFrom.SelectedDate > DateTime.MinValue)
            {
                fromDate = Convert.ToDateTime(txtDateFrom.SelectedDate);
            }
            if (txtDateTo.SelectedDate > DateTime.MinValue)
            {
                toDate = Convert.ToDateTime(txtDateTo.SelectedDate);
            }

            this._useSearch = false;
        }
        bool isAsc = sort.Contains("asc") ? true : false;
        data = OrderManager.GetOrdersPaged(OrderStatusType.CCFail.ToString(), sort.Replace("asc", "").Replace("desc", ""), isAsc, start, limit, _userId, fromDate, toDate);
        totalCount = OrderManager.GetOrdersPagedCount(OrderStatusType.CCSuccess.ToString(), _userId, fromDate, toDate);
        Store1.DataSource = data;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "EShop.WebService")]
    public void UpdateOrder(int id, string specialInstruction)
    {
        string ModeOfPaymentAccepted = specialInstruction.Trim();
        Order order = OrderManager.GetOrderById(id);

        if (!string.IsNullOrEmpty(ModeOfPaymentAccepted))
        {
            order.PaymentStatus = ModeOfPaymentAccepted;
            order.OrderStatus = OrderStatusType.CCSuccess;
        }
        OrderManager.UpdateOrder(order);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "EShop.WebService")]
    public void DeleteOrders(string ids)
    {
        
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray)
        {
            OrderManager.DeleteOrderById(Convert.ToInt32(id));
        }
        Store1.DataBind();
        
    }

    [DirectMethod(Namespace = "EShop.WebService")]
    public void SearchOrder()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}


